<?php
	require_once (dirname(__FILE__)."/../../config/Config.inc");
	require_once (dirname(__FILE__)."/../../config/api.inc");
	require_once ("load.php");
	
	$parm = requestArrayParm($_REQUEST);
	
	$result['status'] = '0';
	
	if(empty($parm['code']))
	{
		$result['msg'] = '缺少参数code';
		echoJSON($result);
		exit;
	}
	
	if(empty($parm['money']))
	{
		$result['msg'] = '缺少参数money';
		echoJSON($result);
		exit;
	}
	
	if(round(floatval($parm['money']),2) <= 0)
	{
		$result['msg'] = '数据异常';
		echoJSON($result);
		exit;
	}
	
	//查询用户的提现金额
	unset($wheres);

	$wheres['userid'] = $userinfo['id'];
	
	$wheres['money'] = array('>',0);
	
	$wheres['addtime'] =  array('<=',time());
	
	$jySum = _SqlTool::getSum('jyinfo', $wheres, array(),'money');
	
	if(round(floatval($parm['money']),2) > $jySum )
	{
		$result['msg'] = '余额不足';
		echoJSON($result);
		exit;
	}
	
	if(empty($parm['type']) || !in_array($parm['type'], array('bank','alipay')))
	{
		$result['msg'] = '缺少参数type';
		echoJSON($result);
		exit;
	}
	
	if($parm['type'] == 'bank')
	{
		$bank = json_decode($userinfo['bank'],true);
		if(empty($bank['bankNum']))
		{
			$result['msg'] = '缺少银行信息';
			echoJSON($result);
			exit;
		}
	}
	
	if($parm['type'] == 'alipay')
	{
		$alipay = json_decode($userinfo['alipay'],true);
		if(empty($alipay['alipayNum']))
		{
			$result['msg'] = '缺少支付宝信息';
			echoJSON($result);
			exit;
		}
	}
	
	unset($wheres);
	
	$wheres['phone'] = $userinfo['loginname'];
	$wheres['today'] = strtotime(date('Y-m-d'));
	if($uservalid = _SqlTool::getInfo('validcode', $wheres))
	{
		
		if($uservalid['errornums'] >= 5)
		{
			$result['msg'] = '验证码错误次数过多';
			echoJSON($result);
			exit;
		}
		
		if($uservalid['code'] == $parm['code'])
		{
			//删除验证码
			unset($query);
			$query['phone'] = $userinfo['loginname'];
			_User::delUserValidCode($query);
			
			unset($tixian,$fanli);
			$tixian['name'] = $userinfo['name'];
			$tixian['phone'] = $userinfo['loginname'];
			$tixian['userid'] = $userinfo['id'];
			$tixian['type'] = $parm['type'];
			
			if($parm['type'] == 'bank')
			{
				$tixian['zhanghao'] = $bank['bankNum'];
			}
			
			if($parm['type'] == 'alipay')
			{
				$tixian['zhanghao'] = $alipay['alipayNum'];
			}
			
			$tixian['money'] = round(floatval($parm['money']),2);
			$tixian['moneyed'] = round(floatval($parm['money']),2);
			$tixian['addtime'] = time();
			$tixian['status'] = '1';
			
			$fanli['userid'] = $userinfo['id'];
			$fanli['money'] = 0-round(floatval($parm['money']),2);
			$fanli['title'] = '提现';
			$fanli['addtime'] = time();
			$resadd = _SqlTool::autoTixian($tixian, $fanli);
			if($resadd)
			{
				$result['status'] = '1';
				$result['msg'] = '申请成功';
			}
			else
			{
				$result['msg'] = '申请失败';
			}
			
			echoJSON($result);
		}
		else
		{
			unset($query,$data);
			$query['id'] = $uservalid['id'];
			$data['errornums'] = $uservalid['errornums']+1;
			
			_User::setUserValidCode($query, $data);
			$result['msg'] = '验证码输入错误';
			echoJSON($result);
			exit;
		}
	}
	else
	{
		$result['msg'] = '请先获取验证码';
		echoJSON($result);
		exit;
	}